package com.tianji.learning.task;

import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.enums.LessonStatus;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class ListenLessonExpire {

    private final ILearningLessonService learningLessonService;


    @Scheduled(cron = "0 0 0 * * ?")//每天0点执行
    public void listen(){
        List<LearningLesson> list = learningLessonService.lambdaQuery()
                .in(LearningLesson::getStatus, LessonStatus.LEARNING, LessonStatus.NOT_BEGIN, LessonStatus.FINISHED)
                .list();
        if (CollUtils.isEmpty(list)){
            log.info("课表中大哥课程均已过期或者没有课程");
            return;
        }
        for (LearningLesson lesson : list) {
            if (lesson.getExpireTime().isBefore(LocalDateTime.now())){
                learningLessonService.lambdaUpdate()
                        .eq(LearningLesson::getId, lesson.getId())
                        .set(LearningLesson::getStatus, LessonStatus.EXPIRED)
                        .update();
            }
        }
    }
}
